unit cuSorting;

interface

type
  TOrder = (ORDER_ASC, ORDER_DEC);

  TSortItem = record
    Index : integer;

    Value  : integer;
    ValueF : real;
    ValueS : string;
  end;

  TSortList = class( TObject )
  public
    Items   : array of TSortItem;
    Count   : integer;

    SortOrder : TOrder;

    constructor Create();
    destructor Destroy(); override;

    procedure Clear();
    procedure setArray(nArraySize:integer);
    procedure setValue(theArrayID,theIndex,theValue:integer);
    procedure setValueF(theArrayID,theIndex:integer;theValueF:real);

    function compareValue():boolean;
    procedure SortInt();//Bubble Sort

    function compareValueF(Value1,Value2:real):boolean;
    procedure SortFloat();//Bubble Sort
    procedure ShellSortFloat();
 end;


implementation

uses cuDebug, SysUtils;

var
  Iteration  : Integer;
  Distance   : integer;

constructor TSortList.Create();
begin
  inherited;

  SortOrder := ORDER_DEC;

  Clear();
end;
destructor TSortList.Destroy();
begin
  Clear();

  inherited Destroy();
end;

procedure TSortList.Clear();
begin
  setArray( 0 );
end;

procedure TSortList.setArray(nArraySize:integer);
begin
  DevConsole.Print( LOG_DEBUG3, '[TSortList] Setting New Array of #'+IntToStr(nArraySize) );

  Count := nArraySize;
  SetLength( Items, Count );

  DevConsole.Print( LOG_DEBUG3, '[TSortList] Confirmation of Array Size #'+IntToStr(High(Items)) );
end;

procedure TSortList.setValue(theArrayID,theIndex, theValue:integer);
begin
  Items[theArrayID].Index  := theIndex;
  Items[theArrayID].Value := theValue;
end;
procedure TSortList.setValueF(theArrayID,theIndex:integer;theValueF:real);
begin
  if ( (theArrayID < 0) or (theArrayID > Count-1) )  then
  begin
    DevConsole.Print( LOG_DEBUG3, '[TSortList] Index out of bounds #'+IntToStr(theArrayID)+' (High #'+IntToStr(High(Items))+')' );
//    DevConsole.saveLog();
    exit;
  end;

  Items[theArrayID].Index  := theIndex;
  Items[theArrayID].ValueF := theValueF;
end;
function TSortList.compareValue():boolean;
begin
end;
procedure TSortList.SortInt();//Bubble Sort
// Sorts Drivers and Teams leader board.
var
  I :integer;
  Sorted : boolean;
  Temp : TSortItem;
begin
  Sorted := TRUE;
  while Sorted do
  begin
    Sorted := FALSE;
    for i := 0 to High(Items)-1 do
    begin
      if (Items[I].Value < Items[I+1].Value) then
      begin
        Temp       := Items[I+1];
        Items[I+1] := Items[I];
        Items[I]   := Temp;

        Sorted := TRUE;
      end;
    end;
  end;
end;

function TSortList.compareValueF(Value1,Value2:real):boolean;
begin
  if (Value1 < Value2) then result := TRUE
  else
    result := FALSE;

  if (SortOrder = ORDER_ASC) then result := not result;
end;
procedure TSortList.SortFloat();//Bubble Sort
// Sorts Drivers and Teams leader board.
var
  I :integer;
  Sorted : boolean;
  Temp : TSortItem;
begin
  Sorted := TRUE;
  while Sorted do
  begin
    Sorted := FALSE;
    for i := 0 to High(Items)-1 do
    begin
      if ( compareValueF( Items[I].ValueF, Items[I+1].ValueF) ) then
      begin
        Temp       := Items[I+1];
        Items[I+1] := Items[I];
        Items[I]   := Temp;

        Sorted     := TRUE;
      end;
    end;
  end;
end;

procedure TSortList.ShellSortFloat();

  procedure SortArray(Distance : Integer);
  var X, Y : Integer; TempItem:TSortItem;
  begin   {Sort}
    Iteration := 0;
    for Y := Distance + 1 to High( Items )+1 do
    begin  {For}
      X := Y - Distance;
      while X > 0 Do
      begin   {While}
        if ( compareValueF( Items[X-1+Distance].ValueF, Items[X-1].ValueF) ) then
//        if Items[X-1+Distance].Value > Items[X-1].Value then
        begin   {if}
          TempItem:= Items[X-1+Distance];

          Items[X-1+Distance] := Items[X-1];

          Items[X-1] := TempItem;

          Dec(X, Distance);
          Inc(Iteration)
        end     {if}
        else
          X := 0;
      end;    {While}
    end    {For}
  end;    {Sort}

begin   {ShellSort}
  Distance := High( Items )+1 div 2;
  While Distance > 0 do
  begin   {While}
    SortArray (Distance);
    Distance := Distance div 2
  end;    {While}
end;    {ShellSort}

end.
